#include "../inc/register.h"

register_widget::register_widget(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	this->SetSoftKeys();
	this->ui.rbMale->setChecked(true);
	this->ui.rbFemale->setChecked(false);
}

register_widget::~register_widget()
{

}

void register_widget::SetSoftKeys(){
	//ui.centralwidget->setContextMenuPolicy(Qt::NoContextMenu);

	QAction* actReg = new QAction(tr("Register"), this);
	actReg->setSoftKeyRole(QAction::PositiveSoftKey);
	addAction(actReg);
	QAction* actBack = new QAction(tr("Return"), this);
	actBack->setSoftKeyRole(QAction::NegativeSoftKey);
	addAction(actBack);

	//QList<QAction *> softKeys;
	//softKeys.append(actReg);
	//softKeys.append(actReg);

	//ui.centralwidget->addActions(softKeys);
	//ui.centralwidget->setFocus();
	connect(actReg, SIGNAL(triggered()), this, SLOT(DtRegister()));
	connect(actBack, SIGNAL(triggered()), this, SLOT(Back()));
}

void register_widget::Back(){
	loginwidget* lw=new loginwidget();
	lw->showMaximized(); 
	this->~register_widget();
}

void register_widget::DtRegister(){
	QString sid=this->ui.tbUserID->text();
	QString spwd=this->ui.tbPwd->text();
	QString stel=this->ui.tbTel->text();
	QString ssex="1";
	if(this->ui.rbFemale->isChecked()){
		ssex="2";
	}
	QUrl u(RegisterUrl);
	u.addQueryItem("uid",QUrl::toPercentEncoding(sid));
	u.addQueryItem("pwd",QUrl::toPercentEncoding(spwd));
	u.addQueryItem("tel",QUrl::toPercentEncoding(stel));
	u.addQueryItem("sex",QUrl::toPercentEncoding(ssex));
	#ifdef _USE_MID_PAGE_
	QString _u=u.toString();
	u=QUrl(_MID_PAGE_);
	u.addQueryItem("url",QUrl::toPercentEncoding(_u));
	#endif
	dh=new DtHttp(u,GET,this);
	connect(dh->Send(),SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply*)));
}

void register_widget::on_rbMale_clicked(){
	this->ui.rbFemale->setChecked(!this->ui.rbMale->isChecked());
}

void register_widget::on_rbFemale_clicked(){
	this->ui.rbMale->setChecked(!this->ui.rbFemale->isChecked());
}


void register_widget::finished(QNetworkReply *reply){
    if(reply->error() == QNetworkReply::NoError)
    {
		QString xml=reply->readAll();
		this->ReadXml(xml);
    }
    else
    {
		QMessageBox::critical(this,
								  "error",
								  "network fail",
								  QMessageBox::Ok);
		return;
    }
}

void register_widget::ReadXml(const QString& xml){
	QDomDocument doc("mydocument"); 
	if (!doc.setContent(xml)) 
	{        
		QMessageBox::critical(this,
							  "error",
							  "network fail",
							  QMessageBox::Ok);  
		return;      
	}
	QDomElement ele;
    QDomElement docElem = doc.documentElement(); 
	QDomElement eleEr=docElem.firstChildElement("error");
	ele=eleEr.firstChildElement("code");
	if(ele.text().trimmed()=="1"){
		QString desc=eleEr.firstChildElement("desc").text();
		QMessageBox::critical(this,
							  "error",
							  desc,
							  QMessageBox::Ok);  
		return;  
	}
	ele=docElem.firstChildElement("guid");
	QString g=ele.text().trimmed();
	dt::Set(_GUID_,g);
	ele=docElem.firstChildElement("token");
	QString t=ele.text().trimmed();
	dt::Set(_TOKEN_,t);
	QMessageBox::critical(this,
						  "done",
						  "register donoe",
						  QMessageBox::Ok);  
}

